package com.icbc.digitalwallet.controller;


import com.icbc.digitalwallet.common.util.JwtUtils;
import com.icbc.digitalwallet.common.util.R;
import com.icbc.digitalwallet.pojo.VO.TransVO;
import com.icbc.digitalwallet.pojo.entity.BankAccount;
import com.icbc.digitalwallet.pojo.entity.Card;
import com.icbc.digitalwallet.pojo.entity.DigitalWallet;
import com.icbc.digitalwallet.pojo.entity.TransFlow;
import com.icbc.digitalwallet.service.DigitalWalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tx
 * @since 2021-07-17
 */
@RestController
@RequestMapping("/digitalWallet")
@Api(tags = "数字钱包")
@Slf4j
public class DigitalWalletController {

    @Autowired
    private DigitalWalletService digitalWalletService;


    @GetMapping("/getUserInfo")
    @ApiOperation("数字钱包首页")
    public R getUserInfo(HttpServletRequest request){

        String token = request.getHeader("token");
        String id = JwtUtils.getUserId(token);
        DigitalWallet wallet = digitalWalletService.getUserInfoById(id);
        return R.ok().data("userInfo",wallet);
    }


    @GetMapping("/getRechargeKey")
    @ApiOperation("获取充值key")
    public R getRechargeKey(){
        String rechargeKey = digitalWalletService.getRechargeKey();
        return R.ok().data("rechargeKey",rechargeKey);
    }

    @PostMapping("/reCharge")
    @ApiOperation("数字钱包充值 银行卡-->数字钱包")
    public R reCharge(HttpServletRequest request,
                      @RequestBody TransVO transVO){

        String token = request.getHeader("token");
        String id = JwtUtils.getUserId(token);
        digitalWalletService.reCharge(id,transVO);
        return R.ok().message("充值成功");
    }


    @PostMapping("/withDraw")
    @ApiOperation("/数字钱包提现 数字钱包-->银行卡")
    public R withDraw(HttpServletRequest request,
                      @RequestBody TransVO transVO){

        String token = request.getHeader("token");
        String id = JwtUtils.getUserId(token);
        digitalWalletService.withDraw(id,transVO);
        return R.ok().message("提现成功");

    }


    @GetMapping("/getTransFlow")
    @ApiOperation("获取交易明细")
    public R getTransFlow(HttpServletRequest request, @ApiParam("交易类型") @RequestParam(value = "type",required = false)Integer type ){

        log.info("获取交易明细");
        String token = request.getHeader("token");
        String id = JwtUtils.getUserId(token);
        List<TransFlow> transFlowList = digitalWalletService.getTransFlow(id,type);
        return R.ok().data("transFlowList",transFlowList);
    }

    @GetMapping("/getCardsInfo")
    @ApiOperation("根据数字钱包账户id查询其绑定的银行卡")
    public R getCardsInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        String id = JwtUtils.getUserId(token);
        List<BankAccount> BankAccountList = digitalWalletService.getCardsInfo(id);
        return R.ok().data("cardList",BankAccountList);
    }

}

